﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace HomagChina.CCHCS.ViewModels
{
    public class StockInfoViewModel : NotifyPropertyChangedBase
    {
        private InsertInfoHandle Handle_Insert;
        private SearchInfoHandle Handle_Search;
        private DeleteInfoHandle Handle_Delet;
        private CommonHandle Handle_Common;
        private bool _IsDeleteVisible = true;
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }
        public RelayCommand DeleteCommand { get; }

        private T_CCHS_Stock stockModel { get; set; }

        private ObservableCollection<StockDetailModel> _List_StockInfoModel;

        public ObservableCollection<StockDetailModel> List_StockInfoModel
        {
            get => _List_StockInfoModel;
            set { _List_StockInfoModel = value; OnPropertyChanged(); }
        }

        public bool IsDeleteVisible
        {
            get => _IsDeleteVisible;
            set { _IsDeleteVisible = value; OnPropertyChanged(); }
        }

        private DispatcherTimer UpdateSessionTimer;

        public event EventHandler RequestScrollPositionSave;

        public event EventHandler RequestScrollPositionRestore;

        public StockInfoViewModel(T_CCHS_Stock model)
        {
            Handle_Insert = new InsertInfoHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Delet = new DeleteInfoHandle();
            Handle_Common = new CommonHandle();
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);
            DeleteCommand = new RelayCommand(Delete);
            stockModel = model;
            Quary();
        }

        /// <summary>
        ///
        /// </summary>
        private void ViewloadedCommand()
        {
            UpdateSessionTimer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(5000)
            };
            UpdateSessionTimer.Tick += UpdateSessionTimer_Tick;
            UpdateSessionTimer.Start();
        }

        private void UpdateSessionTimer_Tick(object sender, EventArgs e)
        {
            RequestScrollPositionSave?.Invoke(this, EventArgs.Empty);
            Quary();
            RequestScrollPositionRestore?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        /// Unloaded event
        /// </summary>
        private void ViewUnloadedCommand()
        {
            UpdateSessionTimer.Stop();
        }

        public void Quary()
        {
            List_StockInfoModel = Handle_Search.GetStockInfo(stockModel);
        }

        public void Delete(object obj)
        {
            StockDetailModel model = obj as StockDetailModel;
            try
            {
                Handle_Delet.DeleteStockDetail(model.Id);
                Quary();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}